#!/bin/bash
# shellcheck disable=SC2034
RUNNING_USER=root
PATTERN_PATH=/mnt/e/project-pattern/
# 安装
 if [[ -z "$(docker images -q rocketmqinc/rocketmq:4.4.0 2> /dev/null)" ]]; then
    docker pull rocketmqinc/rocketmq:4.4.0
 fi

 if [[ -z "$(docker images -q foxiswho/rocketmq:broker-4.4.0 2> /dev/null)" ]]; then
    docker pull foxiswho/rocketmq:broker-4.4.0
 fi

#	运行
  if [[ -z "$(docker ps -aqf "name=rmqnamesrv")" ]]; then
    mkdir -p  /opt/rocketmq/namesrv/logs /opt/rocketmq/logs /opt/rocketmq/store
    mkdir -p  /opt/rocketmq/rocketmq-console-ng/logs
  #运行 mqnamesrv和
    docker run -d -p 23000:9876 \
          --name rmqnamesrv --restart=always \
          -v /opt/rocketmq/logs:/home/rocketmq/logs \
          -v /opt/rocketmq/store:/home/rocketmq/store \
          --add-host=project-pattern:192.168.0.101 \
          rocketmqinc/rocketmq:4.4.0 sh mqnamesrv
  else
      echo "rmqnamesrv running"
  fi

  if [[ -z "$(docker ps -aqf "name=rmqbroker")" ]]; then
    #运行 broker
      docker run -d -p 10911:10911 -p 10909:10909 \
      --name rmqbroker --restart=always \
      -e "NAMESRV_ADDR=project-pattern:23000" -e "JAVA_OPTS=-Duser.home=/opt" \
      -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m" \
      --add-host=project-pattern:192.168.0.101 \
      foxiswho/rocketmq:broker-4.4.0
      docker cp $PATTERN_PATH/.environment/init/docker/rocketmq/broker.conf rmqbroker:/etc/rocketmq/broker.conf
    #进入容器
      docker restart rmqbroker
  else
      echo "rmqbroker running"
  fi


  if [[ -z "$(docker ps -aqf "name=rocketmq-console")" ]]; then
  #运行 mqnamesrv和console
    docker run -d -p 8087:8080 \
          --name rocketmq-console --restart=always \
          --add-host=project-pattern:192.168.0.101 \
          -e "JAVA_OPTS=-Drocketmq.namesrv.addr=project-pattern:23000 -Dcom.rocketmq.sendMessageWithVIPChannel=false" \
          -t styletang/rocketmq-console-ng
  else
      echo "rocketmq-console-ng running"
  fi
